<?php

abstract class Vnf_Controller_Action extends Zend_Controller_Action
{

    public function init() {

        parent::init();
        $controlname = $this->_request->getControllerName();
        $actionname = $this->_request->getActionName();
        //$config = $conf->$controlname->$actionname;
        $translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');

        $this->view->title = $translate->_("$controlname.$actionname.title");
        $this->view->description = $translate->_("$controlname.$actionname.description");
        $this->view->keywords = $translate->_("$controlname.$actionname.keywords");

        if ($this->view->title == "$controlname.$actionname.title") {
            $this->view->title  = $translate->_("default.title");
        }
        if ($this->view->description == "$controlname.$actionname.description") {
            $this->view->description  = $translate->_("default.description");
        }
        if ($this->view->keywords == "$controlname.$actionname.keywords") {
            $this->view->keywords  = $translate->_("default.keywords");
        }

    }
    //protected $layoutPath = BASE_DIR . '/layouts/user/';
//    protected $layout = 'index';

    /**
     * Set layout
     *
     * @param $layout
     * @param $layoutPath
     */
    public function setLayout($layout = 'index', $layoutPath = null) {

        $layoutPath = $layoutPath == null? BASE_DIR . '/layouts/' . CURR_APP_PATH . '/':$layoutPath;
        Zend_Layout::startMvc(array(
            'layoutPath' => $layoutPath,
            'layout' => $layout
        ));
    }

    /**
     * Get param from URL or get from pre URL (use in login form)
     *
     * @param $lparamName
     * @param $defaultValue
     */
    public function getParam($paramName, $defaultValue = null) {
        $param = $this->_request->getParam($paramName, null);
        if (null === $param) { // has no value
            // get value from login form
            $params = Zend_Json::decode($this->_request->getParam("lastparams", "{}"));
            
            // get from last page
            $param = @$params[$paramName];
            
            // get default value if has not value transfer
            $param = $param?$param:$defaultValue;
        }
        
        return $param;
    }
    

    /*    
     * Get all params from URL or get from pre URL (use in login form)
     *
     * @return array
     */
    public function getParams() {
        $params = $this->_request->getParams();
        if (isset($params['lastparams'])) { // has no value
            // get value from login form
            $params = Zend_Json::decode($this->_request->getParam("lastparams", "{}"));
        }
        
        return $params;
    }
    
    public function processLoginAction() {
        $defaultNamespace = new Zend_Session_Namespace('Default');
        $translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');
        if (!isset($defaultNamespace->access) || !$defaultNamespace->access) {
            $loginForm = $this->getParam('loginform', 0);
            $loginPage = $this->getParam('loginpage', 0);
            $defaultNamespace->access = false;
            if ($loginForm) {
                $params = $this->_request->getParams();

                $user = @$params['user'];
                $pwd = @$params['password'];

                $userOb = new Customer();
                //$result = $userOb->getRecords("node.username = '{$user}' and node.password = '{$pwd}'");
                $result = $userOb->getRecords("Email = '{$user}' and Password = '{$pwd}'");
                //print_r($result);
                if (count($result) > 0)
                {
                    if('1'==$result[0]['status']){//status =1 usernam active
                        $defaultNamespace = new Zend_Session_Namespace('Default');
                        $defaultNamespace->user = $result[0];
                        $defaultNamespace->access = true;
                        if($loginPage) {
                            //$this->_redirect();
                            $this->_redirect("/user/viewprofile");
                        }
                    }else{
                         $this->view->error = $translate->_('Please active your account');
                    }
                }
                else {
                    $this->view->error = $translate->_('Email or Password invalid');
                }
            }
        }
        $this->view->access = $defaultNamespace->access;
        
        // if has not permission
        // change to login form
        if ($defaultNamespace->access != true) {
            $loginTemplate = "index/login.phtml";
            $this->_helper->viewRenderer->renderScript($loginTemplate);
        }
    }
    public function prepareData() {
        $params = $this->_request->getParams();
        //$currentPageID = 0;
        $category = new Category();
        /*
        $this->view->allFullFriendlyUrl = $allFullFriendlyUrl = $category->getAllFullFriendlyUrl();

        //echo "-----" . stristr($_SERVER['REDIRECT_URL'], BASE_URL);
        if(stristr($_SERVER['REDIRECT_URL'], BASE_URL)) {
            $friendlyUrl = substr($_SERVER['REDIRECT_URL'], strlen(BASE_URL));
            $allFullFriendlyUrl = $this->view->allFullFriendlyUrl;

            //print_r($allFullFriendlyUrl);
            if (isset($allFullFriendlyUrl[$friendlyUrl])) {
                $currentPageID = $allFullFriendlyUrl[$friendlyUrl];
                $this->view->currentPageID = $currentPageID;
            }
        }

        */
        $currentPageID = $this->getParam('cid', $this->view->currentPageID);
        $id = $this->getParam('id', 0); // item id

        // get default content if has not page transfer
        $showDefaultPage = false;
        $getSubPage = false;
        if ($currentPageID <= 0) {
            $root = $category->getRoot();
            $currentPageID = $root->category_id;
            if ($id == 0) $getSubPage = true; // item id
            
            $this->view->showDefaultPage = $showDefaultPage = true;
        }
        $this->view->currentPageID =  $currentPageID;


        $allCategory = $category->getCacheOfCategory();

        $this->view->allCategory = $allCategory;

        $currentPage = $allCategory[$currentPageID];
        $categoryPath = $currentPage['all_parent']!=""?$currentPage['all_parent'].",".$currentPageID : $currentPageID;

        $this->view->categoryPath = explode(',', $categoryPath);
        if (!is_array($this->view->categoryPath)) {
            $this->view->categoryPath = array();
        }
        $this->view->currentPage = $currentPage;

//        print_r($currentPage);

        $childPage = array();
        @$childPage = $currentPage['children'];
        $this->view->currentChild = $childPage;

        if ($getSubPage) {
            if (isset($childPage[0])) {
                $this->view->currentPageID =  $currentPageID = $childPage[0];
                $this->view->currentPage = $allCategory[$currentPageID];
            }
        }
        
        $paramsTransfer = $params;
        unset($paramsTransfer['loginform']);
        if (isset($paramsTransfer['lastparams'])) {
            $this->view->paramsEncode = $paramsTransfer['lastparams'];
            unset($paramsTransfer['lastparams']);
        } else {
            $this->view->paramsEncode = Zend_Json::encode($paramsTransfer);
        }
    }
}